﻿

$(function () {
    // 重置窗口大小
    function resizeWindow() {
        var DEBUG;
        //DEBUG = true;
        if (DEBUG) {
            [].forEach.call($($('*')), function (a) {
                a.style.outline = '1px solid #' + (~~(Math.random() * (1 << 24))).toString(16);
            });
        }
        // console.log('resize...');
        var minHeight = $('body').height() - $('#dataTreeDiv').offset().top - 20;
        $('#dataTreeDiv').css('max-height', minHeight);
        $('#main').css('height', $('body').height() - $('#main').offset().top - 20);
    }
    resizeWindow();
    // resize窗口
    window.onresize = function () {
        resizeWindow();
    };
    var modelApi = getBaseplatformPort ()     
    const userno = getCookie ('portaluserno')
    var laydate,table
    downlist()
    LogShow('');
    //日志列表显示
    function LogShow(search, page, isSecond) {
        var nTotalNum;

        var userName = $.trim($("#txt_username").val());
        var date = $.trim($("#txt_date").val());
        var ip = $.trim($("#txt_IP").val());
        var state = $.trim($("#txt_result").val());
        var Modular = $.trim($("#txt_function").val())
        var data = {
            page: page || 1, // 请求页码: 1,2,3…
            size: 10, // 每页条数: 1,2,3…
        }
        if (search == "search")
        {
            $.extend(data, { 
                time:date || null ,
                userNo:userName || null,
                ip:ip,
                model:Modular || null,
                status:state
                // username: userName,
                // date: date,
                // ip: ip,
                // Modular:Modular,
                // state: state
            });
        }
        $.ajax({
            type: "post",
            async: false,
            // url: "/BaseAshx/Log/LogAshx.ashx",
            url: modelApi + "resource/getMongodbDatas" ,
            data :data,
            datatype: "json",
            success: function (result) { 
                if (result != "") {
                    result = JSON.parse(result);   
                    nTotalNum = result.data.count
                    var resultData = result.data.data
                    var TotalNumber = result.data.data.length;
                    if (TotalNumber == 0) {
                        $('#userTable').html('<div style="height: 100px;line-height:100px;text-align: center;font-size: xx-large;color: #666;">无数据</div>');
                        $('#pagination').html('');
                        return;
                    } else {
                        var html = "";
                        html = "<tbody>" +
                        "<tr class='head' align='center'>" +
                                "<th>操作用户</th>" +
                                "<th>操作时间</th>" +
                                "<th>操作IP</th>" +
                                "<th>操作模块</th>" +
                                "<th>操作方式</th>" +
                                "<th>返回结果</th>" +
                                "<th>返回信息</th>" +
                            "</tr>";
                        for (var i = 0; i < resultData.length; i++) {
                            var strresult = resultData[i].Result;
                            var msg = "";
                            if (strresult == "200") {
                                msg = "成功";
                            } else {
                                msg = "失败";
                            }
                            html += '<tr  align = "center">' +
                                ' <td>' + resultData[i].userNo + '</td>' +
                                 ' <td>' + new Date(resultData[i].DateTime).toISOString().replace("T", " ").replace(".000Z", "") + '</td>' +  
                                  ' <td>' + resultData[i].Ip + '</td>' +  
                                   ' <td>' + resultData[i].Function + '</td>' +
                                    ' <td>' + resultData[i].Operation + '</td>' +
                                     ' <td>' + msg + '</td>' +
                                      ' <td>' + resultData[i].Message + '</td></tr>';
                        } 
                        
                        html += '</tbody>';
                        $('#userTable').html(html);
                        //分页
                        var pageData = {
                            pageTotal: Math.ceil(nTotalNum / 10),
                            pageAmount: 10, // 每页多少条
                            dataTotal: nTotalNum,
                            curPage: 1
                        }; 
                        console.log(pageData);
                        if (isSecond) {
                            return;
                        };
                        new Page({
                            id: 'pagination',
                            pageTotal: pageData.pageTotal, //必填,总页数（最少1页）
                            pageAmount: pageData.pageAmount, //每页多少条
                            dataTotal: pageData.dataTotal, //总共多少条数据（至少1条）
                            curPage: pageData.curPage, //初始页码,不填默认为1
                            pageSize: pageData.pageSize, //分页个数,不填默认为5
                            showPageTotalFlag: true, //是否显示数据统计,不填默认不显示
                            showSkipInputFlag: true, //是否支持跳转,不填默认不显示
                            getPage: function (page) { 
                                if (search == "search") {
                                    LogShow("search", page, true)
                                } else {
                                    LogShow("", page, true)
                                }
                                
                            }
                        });
                    }

                } else {
                    $('#userTable').html('<div style="height: 100px;line-height:100px;text-align: center;font-size: xx-large;color: #666;">无数据</div>');
                    $('#pagination').html('');
                    return;
                }

            }
        })
    }

    function downlist()
    {
        $.ajax({
            type: "post",
            async: false,
            // url: "/BaseAshx/Log/LogAshx.ashx",
            url: modelApi + "resource/getAllModels",
            data :{},
            success: function (result) {
                if (result != "") {
                    $("#txt_function").html();
                    var html = "";
                    var results = JSON.parse(result);
                    var res = results.data
                    html += '<option value=" ">请选择操作模块</option>';
                    for (var i = 0; i <res. length; i++) {
                        html += '<option value="'+res[i]+'">'+res[i]+'</option>';
                    }
                    $("#txt_function").html(html);
                } else {
                    layui.layer.msg("操作模块下拉为空", {
                        shade: 0.3,
                        time: 1000 // 1秒关闭
                    });
                    return;
                }
            }
        })
    }

    $('#btn_search').on('click', function () {
        LogShow('search');
    });
    //时间控件
  
    layui.use(['laydate','table'], function () {
        laydate = layui.laydate;
        table = layui.table;  
        laydate.render({
            elem: '#txt_date' 
        });
    }); 
})